package com.cheyun.netsalev3.viewmodel

import android.content.Context
import android.content.Intent
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.MutableLiveData
import com.cheyun.netsalev3.bean.DaoDianDetailsParam
import com.cheyun.netsalev3.bean.Data
import com.cheyun.netsalev3.bean.FollowParam
import com.cheyun.netsalev3.bean.KeyValueParam
import com.cheyun.netsalev3.bean.drive.ReserveInfo
import com.cheyun.netsalev3.repository.CustomerStorePepository
import com.cheyun.netsalev3.view.AddClueActivity
import com.cheyun.netsalev3.view.AddDriveActivity
import com.cheyun.netsalev3.view.defeatdetails.DefeatDetailsActivity
import com.cheyun.netsalev3.widget.MyContentDialog
import java.io.Serializable

class CustomerStoreActivityViewModel(var customerStorePepository: CustomerStorePepository) : BaseViewModel() {
    var data = MutableLiveData<List<KeyValueParam>>()
    var sex=0
    var daoDianDetail:DaoDianDetailsParam? = null
    var daoDianDetailsParam = MutableLiveData<DaoDianDetailsParam>()
    var namelist = arrayOf("电         话：",
            " 到店时间：",
            " 离店时间：",
            " 接待时长：",
            " 到店人数：",
            " 接待人员：",
            " 线索拥有者：",
            " 是否首次进店：",
            " 是否邀约：",
//            " 关注品牌：",
            " 关注车系：",
            " 关注车型：",
            " 是否试驾："
    )

    init {
        loadData()
    }

    fun loadData(){
        customerStorePepository.getdata(this::setData)
    }
    fun setData(daoDianDetailsParam: DaoDianDetailsParam) {
        this.daoDianDetail = daoDianDetailsParam
        this.daoDianDetailsParam.postValue(daoDianDetailsParam)
        sex=daoDianDetailsParam.sex

        var daodian = if (daoDianDetailsParam.is_first == 1) "是" else {
            "否"
        }
        //is_invite  是否属邀约 1 是 0 否
        var yaoyue = if (daoDianDetailsParam.is_invite == 1) "是" else {
            "否"
        }
        var is_driving = if (daoDianDetailsParam.is_driving == 1) "是" else {
            "否"
        }
        var valuelist = arrayOf(
                daoDianDetailsParam.phone.toString(),
                daoDianDetailsParam.arrivetime.toString(),
                daoDianDetailsParam.leavetime.toString(),
                daoDianDetailsParam.reception.toString(),
                daoDianDetailsParam.arrpnum.toString(),
                daoDianDetailsParam.rcvname.toString(),
                daoDianDetailsParam.ownername.toString(),
                daodian,
                yaoyue,
//                daoDianDetailsParam.brandname.toString(),
                daoDianDetailsParam.seriesname.toString(),
                daoDianDetailsParam.specname.toString(),
                is_driving
        )
        var temlist = ArrayList<KeyValueParam>()
        for (i in 0 until namelist.size) {
            temlist.add(KeyValueParam(namelist[i], valuelist[i]))
        }
        data.postValue(temlist)
    }

    //点击试驾
    fun onClickDriver(view: View){
        //如果已离店，不能进行试驾
        if(!daoDianDetail?.leavetime.isNullOrEmpty()){
            MyToast("客户已离店，无法进行试乘试驾")
            return
        }

        if(daoDianDetail?.phone!!.isEmpty()){
            var intent = Intent(view.context, AddDriveActivity::class.java)
            intent.putExtra("keliuData",daoDianDetail)
            view.context.startActivity(intent)
            return
        }

        var moth: (d: ReserveInfo) -> Unit = {
            var intent = Intent(view.context, AddDriveActivity::class.java)
//            if(!it.id.equals("0")) {
//                intent.putExtra("data", it as Serializable)
//            }
            intent.putExtra("data", it as Serializable)
            intent.putExtra("keliuData",daoDianDetail)
            view.context.startActivity(intent)
        }
        customerStorePepository.getScanResult(daoDianDetail?.phone!!,daoDianDetail?.id.toString(), moth)
    }

    var follow:FollowParam?=null
    var context: Context? = null
    /**
     * 点击item
     */
    fun onClickItem(view: View) {
        this.context = view.context

        var moth: (it: FollowParam) -> Unit = {
            this.follow = it
            when (it.revkey) {
                "add" -> {
                    var keliudata = convertData()
                    var intent = Intent(view.context, AddClueActivity::class.java)
                    intent.putExtra("name", daoDianDetail?.uname)
                    intent.putExtra("phone", daoDianDetail?.phone)
                    intent.putExtra("sex", daoDianDetail?.sex)
                    //来源：客流接待
                    intent.putExtra("come", "keliu")
                    intent.putExtra("keliudata", keliudata as Serializable)
                    view.context.startActivity(intent)
                }
                "bazaar" ->{
                    MyToast(it.msg)
                }
                "owner" -> {
                    MyToast(it.msg)
                }
                "check" ->{
                    MyToast(it.msg)
                }
                "follow" -> {
                    var intent = Intent(view.context, DefeatDetailsActivity::class.java)
                    var keliudata = convertData()
                    intent.putExtra("tid", it.tid)
                    intent.putExtra("nkey", "archive.index")
                    //来源：客流接待
                    intent.putExtra("come", "keliu")
                    intent.putExtra("keliudata", keliudata as Serializable)
                    view.context.startActivity(intent)
                }
                "followtip" ->{
                    var context = view.context
                    if (context is AppCompatActivity) {
                        var pop = MyContentDialog.newInstance("线索跟进", it.msg +
                                "是否跟进？")
                        pop.onClickOk = this::popOk
                        pop.show(context.supportFragmentManager, "")
                    }
                }
                else -> {
                    var context = view.context
                    if (context is AppCompatActivity) {
                        var pop = MyContentDialog.newInstance("潜客激活", "该客户电话已经存在潜客(战败)," +
                                "是否激活潜客数据？")
                        pop.onClickOk = this::popOk
                        pop.show(context.supportFragmentManager, "")
                    }
                }
            }
        }
        customerStorePepository.getfollow(daoDianDetail?.id!!, moth)
    }

    /**
     * 弹窗回调
     */
    fun popOk() {
        follow?.apply {
            if (context != null) {
                var intent = Intent(context, DefeatDetailsActivity::class.java)
                if(follow!!.tid.isNullOrEmpty()){
                    intent.putExtra("revkey", follow!!.revkey)
                }else {
                    intent.putExtra("tid", follow!!.tid)
//                    if(follow!!.revkey.isNullOrEmpty()){
//                        intent.putExtra("revkey", follow!!.revkey)
//                    }
                }
                if(follow!!.revkey.equals("follow") || follow!!.revkey.equals("followtip")){
                    intent.putExtra("nkey", "archive.purge")  // 跟进，订车，线索移交
                }else{
//                    intent.putExtra("nkey", "archivepot.index")  // 拨号，激活
                    //拨号，激活 由state ,revkey控制
                    intent.putExtra("nkey", "archive.purge")  // 拨号，激活
                }
                context!!.startActivity(intent)
            }

        }
    }

    fun convertData():Data{
        var keliudata = Data(
                daoDianDetail?.arrivetime!!,
                daoDianDetail?.channel!!,
                daoDianDetail?.fid!!,daoDianDetail?.id!!,
                if(daoDianDetail?.leavetime.equals("")) 0 else 1,
                daoDianDetail?.invite_name!!,
                daoDianDetail?.leavetime!!,
                0,
                daoDianDetail?.ownername!!,
                daoDianDetail?.phone!!,
                daoDianDetail?.rcvname!!,
                daoDianDetail?.sex!!,
                daoDianDetail?.tid!!,
                daoDianDetail?.uname!!,
                daoDianDetail?.sharename!!,
                daoDianDetail?.is_driving!!
        )
        return keliudata
    }
}